//计时器



function timer(){
	this.param = {
		container_id:'',
		start:0,
		now:0,
		end:0,
		callback:null,
	};
	this.interval = null;
}

timer.prototype = {
	showTime:function(p){
		$.extend(this.param,p);
//		console.log(this.param);
		var date1 = null;
		var date2 = null;
		var html = '';
		var container = $(this.param.container_id).find(".time-container");
		if(this.param.now>this.param.end){
			clearInterval(this.interval);
		}else if(this.param.now==this.param.end){
			clearInterval(this.interval);
		}else if(this.param.now>=this.param.start){
			html = '<span class="floatLeft" style="margin-right: 0.1rem;">距结束还剩:</span>'
			date1 = this.param.end;
			date2 = this.param.now;
		}else if(this.param.now<this.param.start){
			html = '<span class="floatLeft" style="margin-right: 0.1rem;">距开始还剩:</span>'
			date1 = this.param.start;
			date2 = this.param.now;
		}
		var date3 = date1 - date2;
		var days=Math.floor(date3/(24*3600*1000));
		//计算出小时数
		var leave1=date3%(24*3600*1000);    //计算天数后剩余的毫秒数
		var h=ch=Math.floor(leave1/(3600*1000));
		//计算相差分钟数
		var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
		var m=cm=Math.floor(leave2/(60*1000));
		//计算相差秒数
		var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
		var s=cs=Math.round(leave3/1000);
		if(h<10){
			ch='0'+h;
		}
		if(m<10){
			cm = '0'+m;
		}
		if(s<10){
			cs = "0"+s;
		}
		if(days>0){
			if(this.param.now<this.param.start){
				html = '<p>距开始还剩'+days+'天</p>';
			}else if(this.param.start<=this.param.now){
				html = '<p>距结束还剩'+days+'天</p>';
			}
			if(days>=1){
				clearInterval(this.interval);
			}
		}else{
			if(this.param.now>=this.param.end){
				html = '活动结束';
				container.html(html);
				this.param.callback&&this.param.callback();
				return false;
			}else{
				
				
				html+= 		
				'<span class="time-num">'+ch+'</span>'+
				'<span class="time-span">:</span>'+
				'<span class="time-num">'+cm+'</span>'+
				'<span class="time-span">:</span>'+
				'<span class="time-num">'+cs+'</span>';
			}
		}
		container.html(html)
		this.param.now = this.param.now+100;
		return this;
	},
	countDown:function(){
		var _this = this
		_this.interval = setInterval(function(){
			_this.showTime();
		},100)
	}
}
